package org.apaydin;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

public abstract class BaseFetcher {

    protected int parseInt(String value) {
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    protected TagNode evaluateXPath(TagNode node, String exp, int index) throws XPatherException {
        List<TagNode> tagNodes = evaluateXPath(node, exp);
        return tagNodes.get(index);
    }

    protected List<TagNode> evaluateXPath(TagNode node, String exp) throws XPatherException {
        Object[] objects = node.evaluateXPath(exp);
        List<TagNode> collection = new ArrayList<TagNode>();
        if (objects != null) {
            for (Object o : objects) {
                collection.add((TagNode) o);
            }
        }
        return collection;
    }


    protected int extractPagecount(List<TagNode> as) {
        for (TagNode a : as) {
            int pageCount = parseInt(a.getText().toString());
            if (pageCount > 0) {
                return pageCount;
            }
        }
        return 0;

    }

    protected TagNode getRootTagNode(String url) throws IOException {
        HtmlCleaner cleaner = new HtmlCleaner();

        CleanerProperties props = cleaner.getProperties();
        props.setAllowHtmlInsideAttributes(true);
        props.setAllowMultiWordAttributes(true);
        props.setRecognizeUnicodeChars(true);
        props.setOmitComments(true);

        URLConnection conn = new URL(url).openConnection();

        String encoding = conn.getContentEncoding();
        InputStream inStr;
        if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
            inStr = new GZIPInputStream(conn.getInputStream());
        } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
            inStr = new InflaterInputStream(conn.getInputStream(), new Inflater(true));
        } else {
            inStr = conn.getInputStream();
        }

        InputStreamReader reader = new InputStreamReader(inStr);
        TagNode node = cleaner.clean(reader);
        reader.close();
        conn.getInputStream().close();
        return node;
    }


}
